package com.kcyu.sfherp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcyu.sfherp.entity.Chmx;
import com.kcyu.sfherp.entity.CustomHistoryPrice;
import com.kcyu.sfherp.entity.Good;
import com.kcyu.sfherp.entity.utilEntity.SeriesSaleStatistic;
import com.kcyu.sfherp.mapper.ms.ChmxMapper;
import com.kcyu.sfherp.mapper.ms.CustomHistoryPriceMapper;
import com.kcyu.sfherp.mapper.ms.GoodMapper;
import com.kcyu.sfherp.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private ChmxMapper chmxMapper;

    @Autowired
    private CustomHistoryPriceMapper customHistoryPriceMapper;

    @Override
    public List<Good> queryGoodsByNameOrNumber(String spinfo) {
        List<Good> goods = goodMapper.getGoodsNumber(spinfo);
//        for (Good good2:goods){
//            System.out.println(good2);
//            Good good = goodMapper.GetMSStockByNumber(good2.getSpbh());
//            System.out.println(good);
//            if(good != null){
//                good2.setMsky(good.getMsky());
//            } else {
//                good2.setMsky(good2.getMskc());
//            }
//            Good good1 = goodMapper.GetCKStockByNumber(good2.getSpbh());
//            if(good1 != null){
//                good2.setCkky(good1.getCkky());
//            } else {
//                good2.setCkky(good2.getCkkc());
//            }
//        }
        return goods;

    }

    @Override
    public List<CustomHistoryPrice> queryHistoryPrice(String khname, String spname) {
        return customHistoryPriceMapper.getCustomHistoryPrice(khname,spname);
    }

    @Override
    public List<CustomHistoryPrice> queryRecentlyPriceByspbh(String spbh) {
        return goodMapper.queryRecentlyPricesByspbh(spbh);
    }

    @Override
    public Set<String> getSeriesSet() {
        List<Good> goods = goodMapper.selectList(new QueryWrapper<>());
        return goods.stream().map(Good::getXl).collect(Collectors.toSet());
    }

    @Override
    public List<Good> getSeriesInfo(String xl) {
        QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
        goodQueryWrapper.like("xl", xl);
        return goodMapper.selectList(goodQueryWrapper);
    }

    @Override
    public List<SeriesSaleStatistic> getSeriesSaleStatistic(String xl, String startTime, String endTime) {
        List<Good> seriesInfo = this.getSeriesInfo(xl);
        List<SeriesSaleStatistic> seriesSaleStatistics = new ArrayList<>();
        for (Good good : seriesInfo) {
            SeriesSaleStatistic seriesSaleStatistic = new SeriesSaleStatistic();
            QueryWrapper<Chmx> wrapper = new QueryWrapper<>();
            wrapper.eq("spbh", good.getSpbh());
            wrapper.ge("rq", startTime);
            wrapper.le("rq", endTime);

            List<Chmx> chmxes = chmxMapper.selectList(wrapper);
            seriesSaleStatistic.setGoodId(good.getSpbh());
            seriesSaleStatistic.setGoodName(good.getSpname());
            double v = chmxes.stream().mapToDouble(Chmx::getJs).sum() * Double.valueOf(good.getK()) + chmxes.stream().mapToDouble(Chmx::getXs).sum();
            seriesSaleStatistic.setSalesCount(Double.valueOf(v).floatValue());
            seriesSaleStatistic.setUnitName(good.getDw().trim());
            seriesSaleStatistic.setUnitName2(good.getDw2().trim());
            long js = Math.floorDiv(Double.valueOf(v).longValue(), (long) Math.ceil(Double.parseDouble(good.getK())));
            seriesSaleStatistic.setJs(Long.valueOf(js).floatValue());
            long xs = Math.floorMod(Double.valueOf(v).longValue(), (long) Math.ceil(Double.parseDouble(good.getK())));
            seriesSaleStatistic.setSs(Long.valueOf(xs).floatValue());
            seriesSaleStatistic.setXl(good.getXl());

            seriesSaleStatistics.add(seriesSaleStatistic);
        }

        return seriesSaleStatistics;
    }

    @Override
    public List<Chmx> getSeriesOutDetail(String xl, String startTime, String endTime) {
        List<Good> seriesInfo = this.getSeriesInfo(xl);

        List<Chmx> wholeMxes = new ArrayList<>();
        for (Good good : seriesInfo) {
            QueryWrapper<Chmx> wrapper = new QueryWrapper<>();
            wrapper.eq("spbh", good.getSpbh());
            wrapper.ge("rq", startTime);
            wrapper.le("rq", endTime);
            List<Chmx> chmxes = chmxMapper.selectList(wrapper);
            wholeMxes.addAll(chmxes);
        }

        return wholeMxes;
    }

    @Override
    public List<Good> getAlertGoods() {
        QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
        goodQueryWrapper.eq("orderflag", "要采购")
                .ge("downkc", 0);
        return goodMapper.selectList(goodQueryWrapper);
    }

}
